package main

import (
    "fmt"
    "math"
)

type Circle struct {
    radius float64
}

func (c Circle) getArea() float64 {
    return 3.14 * c.radius * c.radius
}

func swap(x, y string) (string, string) {
    return y, x
}

func swap2(x, y int) int {
    var temp int
    temp = x
    x = y
    y = temp
    return temp
}

func swap3(x *int, y *int) {
    var temp int
    temp = *x
    *x = *y
    *y = temp
}

func main() {
    var a int = 100
    var b int = 200
    var ret int

    ret = max(a, b)
    fmt.Printf("最大值是：%d\n", ret)

    aa, bb := swap("Google", "Runoob")
    fmt.Println(aa, bb)

    // 值传递
    a = 100
    b = 200
    var c int
    fmt.Printf("交换前 a 的值为：%d\n", a)
    fmt.Printf("交换前 b 的值为：%d\n", b)
    // 通过调用函数交换值
    // c := swap2(a, b)
    //c := swap2(b, a)
    // 饮用传递
    swap3(&a, &b)
    fmt.Printf("交换后 a 的值为：%d\n", a)
    fmt.Printf("交换后 b 的值为：%d\n", b)
    fmt.Printf("c 的值为：%d\n", c)

    // 函数作为实参
    getSquareRoot := func(x float64) float64 {
        return math.Sqrt(x)
    }
    fmt.Println(getSquareRoot(9))

    // 闭包
    nextNumber := getSequence()
    fmt.Println(nextNumber())
    fmt.Println(nextNumber())
    fmt.Println(nextNumber())
    nextNumber1 := getSequence()
    fmt.Println(nextNumber1())
    fmt.Println(nextNumber1())

    // 函数方法
    var c1 Circle
    c1.radius = 10.00
    fmt.Println("圆的面积 = ", c1.getArea())

}

func getSequence() func() int {
    i := 0
    return func() int {
        i += 1
        return i
    }
}

func max(num1, num2 int) int {
    var result int

    if (num1 > num2) {
        result = num1
    } else {
        result = num2
    }
    return result
}
